import {SeedInfiniteScroll} from '@shared/components/infinite-scroll/seed-infinite-scroll/seed-infinite-scroll.component';
import {MineService} from '../../mine.service';

import {Subject, Observable} from 'rxjs';

import {Component, OnInit, ViewChild, ViewEncapsulation} from '@angular/core';
import {ToolsService} from '@shared/service/tools/tools.service';
import {AuthService} from '@shared/service/auth/auth.service';
import {imageUtil} from "@core/globals/globals.service";


@Component({
  selector: 'bill',
  templateUrl: './bill.component.html',
  styleUrls: ['./bill.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class BillComponent implements OnInit {
  public dataStore = {
    bill: []
  };
  public stateStore = {
    haveData: true
  };
  public imageUtil = imageUtil.path;
  public params = {
    userId: '',
    page: 1,
    rows: 10
  };
  @ViewChild(SeedInfiniteScroll) infiniteScroll;
  public ngUnsubscribe: Subject<void> = new Subject<void>();

  constructor(public mineSevices: MineService,
              public tools: ToolsService,
              public auth: AuthService) {
    this.params.userId = this.auth.getUserId();
  }

  /**
   * @description 获取钱包账单
   * @author ouyqx-691
   * @date 2017/11/09
   */
  getWalletBill() {
    this.tools.showLoading();
    this.mineSevices.getWalletBill(this.params)
      .subscribe((res) => {
        this.infiniteScroll.complete();
        if (res.result === '0') {
          this.params.page++;

          const bill = res.data.list;
          if (bill.length === 0) {
            this.stateStore.haveData = false;
          } else {
            // this.dataStore.bill = bill;
            this.dataStore.bill = [...this.dataStore.bill, ...res.data.list];
          }
          if (!res.data.hasNextPage) {
            this.infiniteScroll.enable(false);
          }
          // !res.data.hasNextPage && this.infiniteScroll.enable(false);
        }
        this.tools.hideLoading();
      });
  }

  ngOnInit() {
    this.getWalletBill();
  }
}
